/**
 * Copyright (c) 2017-present, Liu Jinyong
 * All rights reserved.
 *
 * https://github.com/huanxsd/MeiTuan  
 * @flow
 */


import React, { PureComponent } from 'react'
import { View, Text, StyleSheet, ScrollView, RefreshControl, TouchableOpacity, ListView, Image } from 'react-native'
import ScrollableTabView, { DefaultTabBar, ScrollableTabBar } from 'react-native-scrollable-tab-view'

import { Heading2, Title, Price } from '../../widget/Text'
import { color, Button, NavigationItem, SpacingView } from '../../widget'
import { screen, system } from '../../common'
import LinearGradient from 'react-native-linear-gradient'
import * as api from '../../api'
import FindItem from './FindItem'
type Props = {
    //navigation: any,
}
type State = {
    isRefreshing: boolean,
    FindList: Array<Object>,
    isRSuccess: boolean,//是否推荐请求成功

}


class FindScene extends PureComponent<Props> {
    static navigationOptions =
        {
            header: null
        };

    constructor(props: Object) {
        super(props)

        this.state = {
            isRefreshing: false,
            findComment: [],
            isFSuccess: true,
        }
        //this.requestData()
    }

    pullRefresh() {
        setTimeout(() => {
            this.requestData()
            this.setState({ isRefreshing: false })
        }, 2000)
    }

    componentDidMount() {
        this.requestData()
    }

    requestData = async () => {
        this.setState({isRefreshing: true })
        this.requestFindComment()
    }
//输入json格式的参数表，返回发现信息列表
    requestFindComment = async () => {
        try {
            var parameter = { pageNum: 1, pageSize: 5 }
            var url = '/Comment/findComment'
            var json = await api.doPost(url, parameter)
            //发现
            listfind = []
            // alert(JSON.stringify(json))
            var listfind = json.data.map(
                (info) => {
                    return {
                        'imageUrl': info.url,
                        'createDate': info.createDate,
                        'text': info.text,
                        'bName': info.bName,
                        'headUrl': info.headUrl,
                        'listImage': info.list_image,
                        'sum': info.sum,
                        'fullname': info.product.full_name,
                        'url': info.url,
                        'compression': info.compression,
                        'list_image': info.product.list_image
                    }
                }
            )
            listfind=[{
                "bName": "asd",
                "compression":  ["http://wx.qlogo.cn/mmopen/PiajxSqBRaEJ9iblOvKb7QyORg2eGeWb0szpCyqxGVzJ8icmRibn52VlaXOgQ2PoK1Ofk9ibVkrdX2vdBXEur9C9blg/132","http://wx.qlogo.cn/mmopen/PiajxSqBRaEJ9iblOvKb7QyORg2eGeWb0szpCyqxGVzJ8icmRibn52VlaXOgQ2PoK1Ofk9ibVkrdX2vdBXEur9C9blg/132"],
                "createDate": 1514892281000,
                "delFlag": "0",
                "gid": "b26fa99e0ffd4a9b9a88565684cd1d64",
                "goodsId": "f8d8bb315a9e457dbca74c888d201c46",
                "headUrl": "http://wx.qlogo.cn/mmopen/PiajxSqBRaEJ9iblOvKb7QyORg2eGeWb0szpCyqxGVzJ8icmRibn52VlaXOgQ2PoK1Ofk9ibVkrdX2vdBXEur9C9blg/132",
                "id": "e1bb140aa4294173b8c3842f5eba62c6",
                "imagesUrls": "",
                "img": [],
                "product": {
                    "detail_name": "",
                    "full_name": "呼呼 K2 ",
                    "id": "b26fa99e0ffd4a9b9a88565684cd1d64",
                    "list_image": "http://wx.qlogo.cn/mmopen/PiajxSqBRaEJ9iblOvKb7QyORg2eGeWb0szpCyqxGVzJ8icmRibn52VlaXOgQ2PoK1Ofk9ibVkrdX2vdBXEur9C9blg/132",
                    "member_price": 256200,
                    "productBaseId": "",
                    "sales_volume": 1,
                    "selling_point": "呼呼",
                    "stock": 100,
                    "type_id": "a6a669ac2c5d492a8c3c083cb6231bb4",
                    "ws_product_id": "f8d8bb315a9e457dbca74c888d201c46"
                },
                "sum": 1,
                "text": "糊糊涂涂",
                "type": "1",
                "updateDate": 1514892281000,
                "url": ["http://wx.qlogo.cn/mmopen/PiajxSqBRaEJ9iblOvKb7QyORg2eGeWb0szpCyqxGVzJ8icmRibn52VlaXOgQ2PoK1Ofk9ibVkrdX2vdBXEur9C9blg/132","http://wx.qlogo.cn/mmopen/PiajxSqBRaEJ9iblOvKb7QyORg2eGeWb0szpCyqxGVzJ8icmRibn52VlaXOgQ2PoK1Ofk9ibVkrdX2vdBXEur9C9blg/132"],
                "userId": "11178241"
            }]
            // alert(JSON.stringify(listfind))
            this.setState({
                findComment: listfind,
                isFSuccess: true,
                isRefreshing:false,
            })
        } catch (error) {
            this.setState({
                isFSuccess: false,
                isRefreshing:false,
            })
        }
    }

    render() {

        return (
            <View style={styles.allcontainer}>
                {/*标题*/}
                <LinearGradient colors={['#FACF5B', '#FAC858', '#FBBC53', '#FBB558', '#FAAD6B', '#F9A87A', '#F9A387']}
                    start={{ x: 0, y: 0 }} end={{ x: 1, y: 0 }}>
                    <View style={styles.headerTitle}>
                        <Text style={styles.textStyle} >发现</Text>
                    </View>

                </LinearGradient>
                {/*发现的内容*/}
                {this.state.isFSuccess ? <View style={styles.container}>
                    <ScrollView
                        refreshControl={
                            <RefreshControl
                                refreshing={this.state.isRefreshing}
                                onRefresh={() => this.requestData()}
                                tintColor='gray'
                            />
                        }>
                        {/*发现部分展示*/}
                        <View>
                            {this.state.findComment.map((item, i) => (
                                <FindItem
                                    info={item}
                                    key={i}

                                />
                            ))}
                        </View>

                    </ScrollView>
                </View>
                    : <TouchableOpacity style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}
                        onPress={() => this.requestData()}>
                        <Text color={'#FFFF00'}>加载失败!</Text>
                        <Text color={'#FFFF00'}>重新加载</Text>
                    </TouchableOpacity>
                }
            </View>
        )
    }
}






const styles = StyleSheet.create({
    allcontainer: {
        flex: 1,
        backgroundColor: 'white',
    },
    container: {
        flex: 1,
        backgroundColor: 'white',
        marginTop: 10,
    },
    //头部导航栏的样式
    headerTitle: {
        width: screen.width,//设置标题栏的宽度
        height: 35,//设置标题栏的高度
        justifyContent: 'center',//横轴对齐方式
        alignItems: "center",//纵轴对齐方式
        flexDirection: 'row',
        marginTop: screen.statusBarHeight,
    },
    textStyle: {
        fontSize: 20,
        color: 'white',
        fontWeight: 'bold',

    },

})


export default FindScene
